package eureka.consumer.controller;

import cn.hutool.core.date.DateTime;
import eureka.consumer.redissonlock.interfaces.DistributedLockCallback;
import eureka.consumer.redissonlock.interfaces.DistributedLockTemplate;
import eureka.consumer.redissonlock.review.lockinter.DistributeLockTemplate;
import eureka.consumer.redissonlock.review.lockinter.LockCallback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

import static eureka.consumer.util.PdfUtils.pdfout;

/**
 * @Author:RenPu
 * @Date: 2020/8/20 16:03
 * @Version: 1.0
 * @description:
 */

@RestController
@Slf4j
public class EurekaConsumerController {


    @Resource
    private LoadBalancerClient client;

    @Resource
    private RestTemplate restTemplate;

//    @Autowired
//    @Qualifier("redissonLockTemplate")
//    private DistributedLockTemplate distributedLockTemplate;

    @Autowired
    @Qualifier("single")
    private DistributeLockTemplate distributedLockTemplate;


    @GetMapping("/hello")
    public String hello() {

        ServiceInstance choose = client.choose("eureka-client");

        if (choose == null) {
            return "No   available  instance ";
        }

        String format = String.format("http://%s:%s/sayHi", choose.getHost(), choose.getPort());

        log.info("url is {}", format);

        return restTemplate.getForObject(format, String.class);

    }


    @PostMapping("/hello")
    public Firend helloPost() {

        ServiceInstance choose = client.choose("eureka-client");

        if (choose == null) {
            return null;
        }

        Firend firend = new Firend();
        firend.setName("Eureka Consumer !!!!!!");

        String format = String.format("http://%s:%s/sayHi", choose.getHost(), choose.getPort());

        log.info("url is {}", format);


        return restTemplate.postForObject(format, firend, Firend.class);

    }

    @GetMapping("/getPdfFile")
    public void getPdfFile(HttpServletResponse response) throws Exception {


//        //模拟算数错误
//        int i=1/0;


        //模拟空指针异常
        Firend firend = new Firend();
        String name = firend.getName();
        String[] split = name.split(".");


        Map<String, String> map = new HashMap();
        map.put("productName", "葡萄糖");
        map.put("patchNo", "BV465465465");
        map.put("productDate", "2021年07月06日");
        map.put("indate", "四个月");
        map.put("reportNo", "NO.798798012");
        map.put("sinePage", "1");
        map.put("page", "20");
        map.put("checkData", "2021年07月06日");
        map.put("checkProgram", "精度提炼检验");
        map.put("start", "2021年06月10日");
        map.put("end", "2021年07月06日");
        map.put("result", "检验合格");
        map.put("checkResult", "允许批量生产");
        map.put("respondePeople", "徐童");
        map.put("checkAdvice", "表现良好，继续保持");
        map.put("qualityDate", "2021年07月06日");


        Map<String, String> map2 = new HashMap();
        map2.put("img", "C:\\Users\\12795\\Desktop\\testimg.jpg");

        Map<String, Object> o = new HashMap();
        o.put("datemap", map);

        pdfout(o, response);
    }


    @RequestMapping("/testDistributeLock")
    public Integer testDistributeLock(){
       return testDistributeLock("231412312",10);
    }



    public Integer testDistributeLock(String key, int result) {
        return distributedLockTemplate.getLock(new LockCallback<Integer>() {
            @Override
            public String getLockName() {
                String keyName = key;
                // key加锁
                log.info("getResultBylock 获取分布式锁 ----startTime is {},keyName is {}", DateTime.now().toString("yyyy-MM-dd HH:mm:ss.SSS"), keyName);
                return keyName;
            }

            @Override
            public Integer getproess() {
                int finalresult = result;
                finalresult += 1;
                System.out.println("分佈式鎖執行了 ！！！！！！");
                return finalresult;
            }
        });

    }

}
